package com.gitee.openviducn.inspector.common;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * api结果封装
 * @Author: https://gitee.com/wesleyOne
 * @Date: 12.07 2019
 */
@Getter
@Setter
public class ResponseResult<R> {
    public final static ResponseResult OK = new ResponseResult();

    private boolean success = true;
    private String code = "666";
    private String message = "操作成功";
    private String path = null;
    private String t = LocalDateTime.now().toString();
    private R data;

    private ResponseResult() {
    }

    private ResponseResult(R data) {
        this.data = data;
    }

    public static <R> ResponseResult<R> success(){
        return new ResponseResult<>();
    }

    public static <R> ResponseResult<R> success(R data){
        return new ResponseResult<>(data);
    }

    public static <R> ResponseResult<R> error(String code, String msg, String path){
        ResponseResult<R> r = new ResponseResult<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setPath(path);
        r.setSuccess(false);
        return r;
    }

    private static <R> ResponseResult<R> error(String code, String msg){
        return error(code, msg, null);
    }

    public static <R> ResponseResult<R> error(ResponseError responseError){
        return error(responseError.getCode(), responseError.getMsg());
    }

    public static <R> ResponseResult<R> error(ResponseError responseError, String msg){
        return error(responseError, msg, null);
    }

    public static <R> ResponseResult<R> error(ResponseError responseError, String msg, String path){
        if (Objects.isNull(msg)) {
            msg = responseError.getMsg();
        }
        return error(responseError.getCode(), msg, path);
    }
}

